package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class RegDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root"
        );
        System.out.println("与数据库建立链接");
        Statement statement = connection.createStatement();
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你的用户名:");
        String username = scan.nextLine();
        System.out.println("请输入你的密码:");
        String password = scan.nextLine();
        System.out.println("请输入你的昵称:");
        String nickname = scan.nextLine();
        System.out.println("请输入你的年龄:");
        int age = scan.nextInt();
        String sql = "INSERT INTO userinfo (username,password,nickname,age) VALUES "+
                "('"+username+"','"+password+"','"+nickname+"',"+age+")";
        int num = statement.executeUpdate(sql);
        if(num > 0) {
            System.out.println("插入成功");
        }
        System.out.println(sql);
        connection.close();



    }

}
